package view.client;

import view.client.Console;
import model.Hearer;
import view.client.CheckUpdate;
import view.client.CheckChangelog;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import view.client.UpdateInfo;
import view.client.Updater;
import java.awt.Dimension;
import java.awt.Toolkit;

public class Frame extends JFrame implements ActionListener{

    private Hearer cliente;
    private JPanel panelActual;
    private PanelMenu panelMenu;
    private PanelGame panelGame;
    private PanelBattle panelBattle;
    private Container contentPane;
    public boolean isNew;
    private static int SCREEN_W = 640;
    private static int SCREEN_H = 680;

    public Frame() {
        initComponents();       
        this.setLayout(new BorderLayout());        
        this.setTitle("CardAdventureConsole");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
        contentPane = getContentPane();
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (dim.width-SCREEN_W)/2;
        int y = (dim.height-SCREEN_H)/2;
        this.setSize(SCREEN_W, SCREEN_H);
        this.setLocation(x, y);
        panelMenu = new PanelMenu(this);
        menuDc.setEnabled(false);
        panelActual = panelMenu;
        contentPane.add(panelActual);
        try{
            menuUpdate.setEnabled(false);
            CheckUpdate cu = new CheckUpdate(this);
        }catch(Exception e){
            
        }       
        menuDc.addActionListener(this);
        menuSair.addActionListener(this);
        menuChangelog.addActionListener(this);
        menuUpdate.addActionListener(this);        
        this.setResizable(false);
        this.setVisible(true);
        revalidate();
        
        
    }
 
    public void setUpdateEnabled(boolean b){
        menuUpdate.setEnabled(b);
        panelMenu.getBtnLogin().setEnabled(true);
        revalidate();
    }
    public void loadMenu() {
        menuDc.setEnabled(false);
        panelMenu = new PanelMenu(this);
        this.panelActual = getPanelMenu();
        contentPane.removeAll();
        contentPane.add(panelActual);
        validate();
    }

    public void loadGame(PanelGame panelGame) {
        menuDc.setEnabled(true);
        this.panelGame = panelGame;
        this.panelActual = panelGame;
        contentPane.removeAll();
        contentPane.add(panelActual);
        validate();
    }
    public void loadBattle(PanelBattle panelBattle){
        this.panelBattle = panelBattle;
        this.panelActual = panelBattle;
        contentPane.removeAll();
        contentPane.add(panelActual);
        validate();
    }

    public void refresh() {
    }

    public void setDefaultButton(JButton button) {
        this.getRootPane().setDefaultButton(button);
    }

    public void setFocused(JComponent objeto) {
        //this.getRootPane().setFocusable(true);
        //objeto.requestFocusInWindow();
        //InitialFocusSetter.setInitialFocus(this, objeto);
    }

    public void addThread(Hearer thread) {
        this.cliente = thread;
    }

    public Console getConsole() {
        return getPanelGame().getConsole();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        menuChangelog = new javax.swing.JMenuItem();
        menuUpdate = new javax.swing.JMenuItem();
        menuDc = new javax.swing.JMenuItem();
        menuSair = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jMenu1.setText("Game");

        menuChangelog.setText("Changelog");
        jMenu1.add(menuChangelog);

        menuUpdate.setText("Update");
        jMenu1.add(menuUpdate);

        menuDc.setText("Desconectar");
        jMenu1.add(menuDc);

        menuSair.setText("Sair");
        jMenu1.add(menuSair);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 563, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 321, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem menuChangelog;
    private javax.swing.JMenuItem menuDc;
    private javax.swing.JMenuItem menuSair;
    private javax.swing.JMenuItem menuUpdate;
    // End of variables declaration//GEN-END:variables

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource().equals(menuDc)){
            cliente.close();
            loadMenu();
        }
        if (e.getSource().equals(menuUpdate)){
            if (menuUpdate.isEnabled() && !isNew){
                try {
                    new UpdateInfo(Updater.getWhatsNew());
                    System.exit(1);
                } catch (Exception ex) {
                    Logger.getLogger(Frame.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        if (e.getSource().equals(menuChangelog)){
            CheckChangelog cc = new CheckChangelog();
        }
        if (e.getSource().equals(menuSair)){
            System.exit(0);
        }
    }

    public PanelMenu getPanelMenu() {
        return panelMenu;
    }

    public PanelGame getPanelGame() {
        return panelGame;
    }

    public PanelBattle getPanelBattle() {
        return panelBattle;
    }
}
